<?php
/**
 * Created by PhpStorm.
 * User: Adminstratot
 * Date: 2019/4/22
 * Time: 20:59
 */

namespace app\api\controller\v1;

use app\api\model\Banner as BannerModel;
use app\api\validate\IDMustBePostiveInt;
use app\lib\exception\BannerMissException;

class Banner
{
    /**
     * 获取指定id的banner信息
     * @url /banner/:id
     * @http GET
     * @id banner的id号
     */
    public function getBanner($id){
        //    //php think optimize:route 生成路由缓存
        /*$data=[
            'id'    =>  $id
        ];*/

        /*
        //独立验证
        $validate=new Validate([
            'name'  =>  'require|max:10',
            'email' =>  'email'
        ]);
        //验证器
        $validate=new IDMustBePostiveInt();
        $result=$validate->batch()->check($data);
        var_dump($validate->getError());
        */
        //AOP面向切面编程
        (new IDMustBePostiveInt())->goCheck();
        //$banner=BannerModel::with(['items','items.img'])->find($id);
        //get,find,all,select
        //$banner=new BannerModel();
        //$banner=$banner->get($id);
        $banner=BannerModel::getBannerByID($id);
        //$banner->hidden(['update_time','delete_time']);
        //$banner->visible(['id','update_time']);
        if (!$banner){
            throw new BannerMissException();
        }
        /*try{
            $banner=BannerModel::getBannerByID($id);
        }catch (Exception $ex){
            $err=[
                'error_code'=>10001,
                'msg'=>$ex->getMessage()
            ];
            return json($err,400);
        }*/
        //return json($banner);
        return $banner;
    }
}